package com.trainbook.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.trainbook.service.ISiteService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;

/**
 * <p>
 * 站点与站点间相连的表
 * </p>
 *
 * @author Dempsey
 * @since 2020-11-27
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("site2site")
@ApiModel(value = "Site2site", description = "站点与站点间相连的表")
public class Site2site implements Serializable {

    private static final long serialVersionUID = 1L;

    @Autowired
    private ISiteService siteService;

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @ApiModelProperty(value = "站点 A")
    @TableField("siteA")
    private Integer siteA;

    @ApiModelProperty(value = "站点 B")
    @TableField("siteB")
    private Integer siteB;

    @ApiModelProperty(value = "站点间距离,单位km")
    @TableField("distance")
    private Double distance;

    @ApiModelProperty(value = "所需时间")
    @TableField("requiredTime")
    private Long requiredTime;

    @ApiModelProperty(value = "票价")
    @TableField("price")
    private Double price;

    public void setDistance() {
        Site _siteA = siteService.getOne((new QueryWrapper<Site>().eq("id", this.siteA)));
        Site _siteB = siteService.getOne((new QueryWrapper<Site>().eq("id", this.siteB)));
        this.distance = Math.sqrt(Math.pow(_siteA.getLatitude() - _siteB.getLatitude(), 2) +
                Math.pow(_siteA.getLongitude() - _siteB.getLongitude(), 2));

        this.distance = Math.acos(Math.sin(_siteA.getLongitude()) * Math.sin(_siteB.getLongitude())) +
                6371.004 * (Math.cos(_siteA.getLatitude()) * Math.cos(_siteB.getLatitude()) *
                        Math.cos(_siteA.getLongitude() - _siteB.getLongitude()));
        //D =arc cos((sin北纬A×sin北纬B)＋(cos北纬A×cos北纬B×cosAB两地经度差))×地球平均半径 (Shormin)
    }
}
